#include "graphicitemsettingdialog.h"
#include "global.h"

#include <QStandardItemModel>
#include <QModelIndex>

#include <QtGui/QFont>
#include <QtGui/QMessageBox>
#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>

#if DEBUG_ON
    #include <QDebug>
#endif

GraphicItemSettingDialog::GraphicItemSettingDialog(const QString &tableName, QWidget *parent) :
    QDialog(parent)
{
    resize(800,480);
    setWindowTitle(QString("手动点设置"));

    add_grabtn_dlg = new AddGraBtnDialog(this);

    model = new QSqlTableModel(this);
    model->setTable(tableName);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setSort(0,Qt::AscendingOrder);
    model->select();

    model->setHeaderData(0, Qt::Horizontal,QString("编号"));
    model->setHeaderData(1, Qt::Horizontal, QString("区号"));
    model->setHeaderData(2, Qt::Horizontal, QString("栋号"));
    model->setHeaderData(3, Qt::Horizontal, QString("层号"));
    model->setHeaderData(4, Qt::Horizontal, QString("房号"));
    model->setHeaderData(5, Qt::Horizontal, QString("设备名"));
    model->setHeaderData(6, Qt::Horizontal, QString("屏蔽"));

    item_view = new QTableView(this);
    item_view->setModel(model);
    item_view->verticalHeader()->hide();

    submit_btn = new QPushButton(QString("提交更改"));
    revert_btn = new QPushButton(QString("放弃更改"));
    back_btn = new QPushButton(QString("返回"));

    add_item_btn = new QPushButton(QString("添加手控点"));
    delete_item_btn = new QPushButton(QString("删除手控点"));
    set_device_logo = new QPushButton(QString("设置设备图标"));


    connect(revert_btn, SIGNAL(clicked()), this, SLOT(revert()));
    connect(submit_btn, SIGNAL(clicked()), this, SLOT(submit_all()));
    connect(back_btn, SIGNAL(clicked()), this, SLOT(close()));

    connect(delete_item_btn, SIGNAL(clicked()), this, SLOT(on_delete_pushbutton_clicked()));
    connect(add_item_btn, SIGNAL(clicked()), this, SLOT(add_item_btn_clicked()));


    QHBoxLayout *leftlayout = new QHBoxLayout;
    leftlayout->addWidget(item_view);

    QVBoxLayout *rightlayout = new QVBoxLayout;
    rightlayout->addWidget(submit_btn);
    rightlayout->addWidget(revert_btn);
    rightlayout->addWidget(set_device_logo);
    rightlayout->addWidget(add_item_btn);
    rightlayout->addWidget(delete_item_btn);
    rightlayout->addWidget(back_btn);

    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->addLayout(leftlayout);
    mainlayout->addLayout(rightlayout);
    setLayout(mainlayout);
}


void GraphicItemSettingDialog::add_item_btn_clicked()
{
    add_grabtn_dlg->show();
}


void GraphicItemSettingDialog::on_delete_pushbutton_clicked()
{
    QModelIndexList selection = item_view->selectionModel()->selectedRows(0);
    if( selection.empty())
    {
        QMessageBox::information(this, tr("删除手控点"),
				 tr("请选择要删除的手控点！"));
        return;
    }

    QSqlDatabase::database().transaction();
    for(QModelIndexList::Iterator itr = selection.begin(); itr!=selection.end(); ++itr)
    {
        QModelIndex idIndex = *itr;
        QSqlQuery query;
        int id = idIndex.sibling(idIndex.row(),0).data().toInt();
        QString name = idIndex.sibling(idIndex.row(), 5).data().toString();
        if( query.exec( QString("DELETE FROM locate_information_table WHERE id=%1").arg(id) ) )
        {
//            qDebug()<<"delete item: "<<id<<" , "<<name;
        }
    }
    QSqlDatabase::database().commit();

    //submit deletation
    model->database().transaction();
    if ( model->submitAll()  && model->database().commit() )
    {
        QMessageBox::information(this,QString("提示"), QString("删除成功！"));
    }
    else
    {
        model->database().rollback();
        QMessageBox::warning(this, QString("删除手控点"),
			     QString("数据库错误: %1").arg(model->lastError().text()));
    }
}

void GraphicItemSettingDialog::submit_all()
{
    model->database().transaction();
    if ( model->submitAll()  && model->database().commit() )
    {
        QMessageBox::information(this,QString("提示"), QString("提交成功！"));
    }
    else
    {
        model->database().rollback();
        QMessageBox::warning(this, QString("设置手控点"),
			     QString("数据库错误: %1").arg(model->lastError().text()));
    }
}

void GraphicItemSettingDialog::revert()
{
    model->database().transaction();
    model->revertAll();

    if ( !model->database().commit() )
    {
        model->database().rollback();
        QMessageBox::warning(this, QString("设置手控点"),
			     QString("数据库错误: %1").arg(model->lastError().text()));
    }
}
